package com.fs.base.pojo;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Formula;

import com.fs.base.annotation.ColumnDesc;
import com.fs.base.annotation.Echo;
import com.fs.base.param.SysConstant;

@Entity
@Table(name="s_user")
@Data@EqualsAndHashCode(callSuper=false)
@Echo(idField="id", textField="nickName")
@NoArgsConstructor
public class User extends PkEntity{
	public static final String SYS_ADM = "sysAdm";
	@ColumnDesc(name="用户账号")
	private String account;

	@ColumnDesc(name="用户密码")
	private String pwd;
	
	@ColumnDesc(name="手机") 
	private String mobile;
	
	@ColumnDesc(name="年龄") 
	private Integer age;
	
	@ColumnDesc(name="昵称") 
	private String nickName;
	
	@ColumnDesc(name="邮箱")
	private String email;
	
	/*
	 * sys:系统账号
	 * sms:短信账号
	 */
	@ColumnDesc(name="用户账号类型")
	private String accountType;
	
	@ColumnDesc(name="性别") 
	private String gender = "保密";
	
	@ColumnDesc(name="QQ号")
	private String qq;
	
	@ColumnDesc(name="备注")
	@Column(name="remark", columnDefinition="varchar(255) COMMENT '备注'")
	private String remark;
	
	private Integer status = 0;
	/*
	 * 职务
	 */
	private String duty;
	/*
	 *所属项目部 
	 */
	private String dept;
	
	@Transient
	private String deptName;
	public String getDeptName(){
		Set<String> nameSet = new HashSet<String>();
		String[] array = null;
		if(StringUtils.isNotBlank(dept)){
			array = dept.split(",");
			for(String orgNo: array){
				if(StringUtils.isNotBlank(orgNo) && SysConstant.ORG_NO_NAME_MAP.containsKey(orgNo)){
					String orgName = SysConstant.ORG_NO_NAME_MAP.get(orgNo);
					nameSet.add(orgName);
				}
			}
		}
		return nameSet.toString();
	}
	
	/*
	 * 角色名称
	 */
	@Formula("(SELECT GROUP_CONCAT(r.role_name) from s_role r WHERE r.id in( SELECT ur.role_id  FROM s_user_role ur WHERE ur.user_id = id))")
	private String roleNames;
	
	
	public Boolean isSysAdm(){
		return SYS_ADM.equals(this.getAccount());
	}
	
	public User (String loginAccount,String  loginPwd){
		this.account = loginAccount;
		this.pwd = loginPwd;
	}
}
